package cn.iocoder.yudao.module.pay.controller.admin.wallet;

import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.recharge.PayWalletRechargeCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.recharge.PayWalletRechargePageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.recharge.PayWalletRechargeRespVO;
import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletRechargeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 钱包充值")
@RestController
@RequestMapping("/pay/admin-wallet-recharge")
@Validated
@Slf4j
public class AdminPayWalletRechargeController {

    @Resource
    private PayOrderService payOrderService;

    @PostMapping("/recharge")
    @Operation(summary = "后台充值")
    public CommonResult<Boolean> walletRecharge(
            @Valid @RequestBody PayWalletRechargeCreateReqVO reqVO) {

        // 创建订单
        PayWalletRechargeDO walletRecharge = walletRechargeService.createWalletRecharge(
                reqVO.getUserId(), UserTypeEnum.MEMBER.getValue(), "http://127.0.0.1", reqVO.getPayPrice(), reqVO.getBonusPrice(), reqVO.getPackageId(), "店内后台充值");

        // 提交订单
        PayOrderSubmitReqVO submitReqVO = new PayOrderSubmitReqVO();
        submitReqVO.setChannelCode(PayChannelEnum.WALLET.getCode());
        submitReqVO.setDisplayMode(PayOrderDisplayModeEnum.NONE.getMode());
        submitReqVO.setReturnUrl("http://127.0.0.1");
        submitReqVO.setId(walletRecharge.getPayOrderId());
        payOrderService.submitOrder(submitReqVO, "http://127.0.0.1");

        return success(true);
    }

    @Resource
    private PayWalletRechargeService walletRechargeService;

    @PostMapping("/page")
    @Operation(summary = "充值记录页") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    public CommonResult<PageResult<PayWalletRechargeRespVO>> page(@RequestBody PayWalletRechargePageReqVO pageReqVO){
        PageResult<PayWalletRechargeDO> pageResult = walletRechargeService.getOrderPage(pageReqVO);
        return success(PayWalletRechargeConvert.INSTANCE.convertPage(pageResult));
    }

}
